package top.hzy520.online.controller;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.hzy520.common.constants.Constants;
import top.hzy520.common.page.Pagination;
import top.hzy520.common.resp.ApiResult;
import top.hzy520.online.entity.OnlineUser;
import top.hzy520.online.service.OnlineService;

import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 0:22 2018/9/26
 */
@Controller
public class OnlineController {
    @Autowired
    @Qualifier("onlineServiceImpl")
    private  OnlineService onlineService;

    @RequestMapping("online/list")
    @ResponseBody
    public Pagination paginationQueryAllOnlineUserList(int limit, int offset){
        Pagination pagination=new Pagination();
        int count=onlineService.selectAllCount();
        List<OnlineUser> onlineUserList=onlineService.selectAllOnlineUserList(offset,limit);
        pagination.setRows(onlineUserList);
        pagination.setTotal(count);
        return pagination;
    }
    @RequestMapping("/forcelogout/{sessionId}")
    @ResponseBody
    public ApiResult forceLogout(@PathVariable("sessionId") String sessionId) {
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"下线成功");
        Subject subject=new Subject.Builder().sessionId(sessionId).buildSubject();
        subject.logout();
        ThreadContext.remove(ThreadContext.SUBJECT_KEY);
         onlineService.forceLogout(sessionId);
        return apiResult;
    }
    @RequestMapping(value = "/online/delete",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteOnlineUser(@RequestBody List<OnlineUser> onlineUserList){
     ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"删除成功");
     onlineService.deleteOnlineUser(onlineUserList);
     return apiResult;
    }
}
